package org.merak.core.web;

import java.io.IOException;
import java.net.HttpURLConnection;

import org.merak.core.Application;

@SuppressWarnings("serial")
public class HttpErrorException extends HttpException {

	//~ Attributes /////////////////////////////////////////////////////////////////////////////////
	//**********************************************************************************************
	private int    responseCode    = -1;
	private String responseMessage = null;	
	private String contentType     = null;

	//~ Constructors ///////////////////////////////////////////////////////////////////////////////
	//**********************************************************************************************
	public HttpErrorException(HttpURLConnection conn) {
		
		super( readErrorStream(conn) );
		try {
			this.responseCode    = conn.getResponseCode();
			this.contentType     = conn.getContentType();
			this.responseMessage = conn.getResponseMessage();
		} 
		catch (IOException e) {
			throw new HttpException(e);
		}

	}	
	
	//~ Methods ////////////////////////////////////////////////////////////////////////////////////
	//**********************************************************************************************
	private static String readErrorStream(HttpURLConnection conn) {

		try {
			return Application.io.readString(
				conn.getErrorStream(),conn.getContentEncoding(),conn.getContentLength()
			);
		} 
		catch (IOException e) {
			throw new HttpException(e);		
		}
		
	}	
	//**********************************************************************************************
	public String getResponseMessage() {
		return responseMessage;
	}	
	//**********************************************************************************************
	public int getResponseCode() {
		return responseCode;
	}
	//**********************************************************************************************
	public String getContentType() {
		return contentType;
	}
	//**********************************************************************************************
}


